package com.wangy.io;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class StreamUtil {
	
	public static byte[] readToByte(InputStream inStream) throws IOException{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len = inStream.read(buffer)) != -1){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}
	
	public static String readToString(InputStream inStream) throws IOException{
		BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
		StringBuffer result = new StringBuffer();
		String temp = reader.readLine();
		while(temp!=null){
			result.append(temp+"\n");
			temp = reader.readLine();
		}
		inStream.close();
		reader.close();
		return result.toString();
	}
	
	public static void write(OutputStream output,byte[] buf) throws IOException{
		output.write(buf);
		output.close();
	}
	
	public static void write(OutputStream output,String content) throws IOException{
		write(output,content.getBytes());
		output.close();
	}
	
    
	
	
}
